//
//  SaveDetailController.m
//  MyTrip
//
//  Created by zhang on 15/9/1.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "SaveDetailController.h"
#import "DetailHeaderView.h"
#import "AsyncGETConnection.h"
#import "Diary.h"
#import "DiaryDetailHeader.h"
#import "DayCell.h"
#import "DiaryDataBase.h"
#define Width [[UIScreen mainScreen] bounds].size.width

@interface SaveDetailController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *detailTable;
@property (nonatomic, retain) NSMutableArray *detailArray;
@property (nonatomic, retain) DetailHeaderView *detailHeaderView;
@property (nonatomic, retain) NSMutableArray *daysArray;


@end

@implementation SaveDetailController

- (void)dealloc
{
    [_daysArray release];
    [_detailHeaderView release];
    [_detailTable release];
    [_detailArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailArray = [NSMutableArray array];
        self.daysArray = [NSMutableArray array];
    }
    return self;
}
- (void)getMessage{
    [AsyncGETConnection asyncGETConnectionWithURLStr:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%ld/waypoints/", _diary.diaryid] parameters:nil messageBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *user = [dic objectForKey:@"user"];
        DiaryDetailHeader *diary = [DiaryDetailHeader diaryDetailWithHeaderImage:[dic objectForKey:@"cover_image"] authorImage:[user objectForKey:@"avatar_m"] authorName:[user objectForKey:@"name"] title:[dic objectForKey:@"name"] startTime:[dic objectForKey:@"first_day"] days:[[dic objectForKey:@"day_count"] integerValue] mileage:[[dic objectForKey:@"mileage"] integerValue] like:[[dic objectForKey:@"recommendations"] integerValue]];
        [_detailHeaderView setValueWithModel:diary];
        NSArray *days = [dic objectForKey:@"days"];
        [_daysArray addObjectsFromArray:days];
        [_detailTable reloadData];
    }];
    
}




- (void)createTable{
    self.detailTable = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped] autorelease];
    _detailTable.delegate = self;
    _detailTable.dataSource = self;
    [_detailTable registerClass:[DayCell class] forCellReuseIdentifier:@"day"];
    [self.view addSubview:_detailTable];
    
    self.detailHeaderView = [[[DetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 400 * OffHeight)] autorelease];
    _detailTable.tableHeaderView = _detailHeaderView;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _daysArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dic = [_daysArray objectAtIndex:section];
    NSArray *array = [dic objectForKey:@"waypoints"];
    
    return [array count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [_daysArray objectAtIndex:indexPath.section];
    NSArray *array = [dic objectForKey:@"waypoints"];
    NSDictionary *localDic = [array objectAtIndex:indexPath.row];
    return [DayCell hightWithIndexPathOfDic:localDic];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"day"];
    NSDictionary *dic = [_daysArray objectAtIndex:indexPath.section];
    NSArray *array = [dic objectForKey:@"waypoints"];
    NSDictionary *dayDic = [array objectAtIndex:indexPath.row];
    [cell createViewWithDic:dayDic];
    return cell;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self createTable];
    [self getMessage];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
